package com.qf.service.impl;

import com.qf.dao.*;
import com.qf.dao.impl.*;
import com.qf.model.*;
import com.qf.service.OrderService;
import com.qf.util.CodeUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    OrderDao dao=new OrderDaoImpl();
    CarDao carDao=new CarDaoImpl();
    AddressDao addressDao=new AddressDaoImpl();
    OrderDetailDao orderDetailDao=new OrderDetailDaoImpl();
    OrderItemDao orderItemDao=new OrderItemDaoImpl();
    GoodDao goodDao=new GoodDaoImpl();
    @Override
    public int addOrder(String aidStr, String sumStr, int uid) {
        //都是为了插入订单表
        Order o=new Order();
        o.setOnum(CodeUtil.getCode());//生成编号，年月日时分秒+四位随机数
        o.setOstatus(0);//默认为未支付
        o.setUid(uid);
        o.setOmoney(new BigDecimal(sumStr));
        o.setAid(Integer.parseInt(aidStr));
        o.setOtime(new Date());
        int a=dao.insertOrderByOrder(o);
        System.out.println("这是service里面拿到自动增长"+o.getId());
        //接下来是为了插入订单详情表
        //根据uid查询出这个人购物车中有几件商品
        List<Car> cars = carDao.selectCarByUid(uid);
        for(Car c:cars){
            a=orderDetailDao.insertOrderDetail(c.getPid(),c.getCnum(),o.getId());
        }
        //生成订单后，购物车中所以和该人有关的信息应删除了
        //carDao.delCarByUid(uid);

        return a;
    }

    @Override
    public List<Order> getOrders(int uid) {


        List<Order> orders = dao.selectOrdersByUid(uid);
        for(Order o:orders){
            Address addrss=addressDao.selectAddressById(o.getAid());
            o.setAddress(addrss);
        }

        return orders;
    }

    @Override
    public Order getOrderDetails(String oid) {
        Order order = dao.selectOrdersByOid(Integer.parseInt(oid));
        order.setAddress(addressDao.selectAddressById(order.getAid()));
    //接下来需要给list赋值====>select * from orderitem where oid=order.getId();
        List<OrderItem> list=orderItemDao.selectOrderItemsByOid(oid);
        for(OrderItem o:list){
          Goods good=goodDao.selectGoodsById(o.getPid());

            o.setGood(good);
         }



        order.setList(list);


        return order;
    }
}
